package com.ruoyi.system.util;

import com.ruoyi.system.domain.Stock;


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

public class MAUtils {
    private void test(int dayCount) {
        //算法公式:MA(C,X)=(X1+X2+…+XX)/X.
        //MA(C,1)=C/1=1(这里C=1,X=1);
        //MA(C,2)=(1+2)/2=1.5;
        //MA(C,3)=(1+2+3)/3=2;
        //MA(C,4)=(1+2+3+4)/4=2.5;
        //MA(C,5)=(1+2+3+4+5)/5=3.
        List<Data> dataList = new ArrayList<>();
        for (int i = 0; i < dayCount; i++) {
            Data data = new Data();
            data.setClosePrice(i + 1);
            data.setOpenPrice(i + 1);
            dataList.add(data);
        }

        List<Double> maResults = new ArrayList<>();
        for (int days = 0; days < dayCount; days++) {
            double sum = 0;
            for (int dataIdx = 0; dataIdx < dataList.size(); dataIdx++) {
                if (dataIdx > days) {
                    break;
                }
                sum += dataList.get(dataIdx).getOpenPrice();
            }
            maResults.add(sum / (days + 1));
        }
        System.out.println("=============");
        System.out.println(maResults);
    }

    public static List<BigDecimal> calculateMA(int dayCount, List<Stock> data) {
        List<BigDecimal> ma = new ArrayList<>();
        LimitQueue<BigDecimal> limitQueue = new LimitQueue<>(dayCount);

        for (Stock datum : data) {
            if (limitQueue.getQueue().size() < dayCount) {
                limitQueue.add(datum.getTrade());
                continue;
            }
            BigDecimal sum = new BigDecimal(0);
            if (limitQueue.getQueue().size() == dayCount) {
                for (BigDecimal bigDecimal : limitQueue) {
                    sum = sum.add(bigDecimal);
                }
                ma.add(sum.divide(new BigDecimal(dayCount), RoundingMode.HALF_UP));
            }
            limitQueue.offer(datum.getTrade());

        }


//        for (int i = 0; i < data.size(); i++) {
//            if (i < dayCount - 1) {
//                continue;
//            }
//            BigDecimal sum = new BigDecimal(0);
//            for (int count = i; count > i - dayCount; count--) {
//                sum.add(data.get(count).getOpen());
//            }
//            ma.add(sum.divide(new BigDecimal(dayCount)));
//        }

//        for (int days = 0; days < dayCount; days++) {
//            BigDecimal sum = new BigDecimal(0);
//            for (int dataIdx = 0; dataIdx < data.size(); dataIdx++) {
//                if (dataIdx > days) {
//                    break;
//                }
//                sum.add(data.get(dataIdx).getOpen());
//            }
//            ma.add(sum.divide(new BigDecimal(days + 1)));
//        }
        return ma;
    }

    public static void main(String[] args) {
        new MAUtils().test(100);
    }

    class Data {
        Data() {

        }

        double closePrice;
        double openPrice;

        public double getClosePrice() {
            return closePrice;
        }

        public void setClosePrice(double closePrice) {
            this.closePrice = closePrice;
        }

        public double getOpenPrice() {
            return openPrice;
        }

        public void setOpenPrice(double openPrice) {
            this.openPrice = openPrice;
        }

        @Override
        public String toString() {
            return "Data{" +
                    "closePrice=" + closePrice +
                    ", openPrice=" + openPrice +
                    '}';
        }
    }
}